//引入express模块创建服务器
const express = require('express');
const app = express();
//引入fs模块
const fs = require('fs');
//引入path模块
const path = require('path');
//引入body-parser模块并注册中间件
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended:false}));
//引入express-session模块并注册中间件
const session = require('express-session');
app.use(session({
    secret:'123456',
    resave:false,
    saveUninitialized:false //保存不要初始化
}));

//静态资源托管
app.use('/node_modules',express.static('./node_modules'));
//配置ejs
app.set('view engine','ejs');
app.set('views','./views');

// //引入router/index.js模块
// const router1 = require('./router/index');
// app.use(router1);
// //引入router/user.js模块
// const router2 = require('./router/user');
// app.use(router2);
//-------------------------------通过循环引入路由模块-----------------------------
fs.readdir(path.join(__dirname,'./router'),(err,fileNames) => {
    //读取道德router下面的文件夹是以数组的形式存在的
    if(err) return console.log('读取文件失败');
    fileNames.forEach((item) => {
        const router = require(path.join(__dirname,'./router',item));
        app.use(router);
    })
})

//启动服务器
app.listen(80,() => {
    console.log('server running at http://127.0.0.1');
})